package com.budwk.app.sqfwV2.component;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.component.FromSummaryComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.constants.TemplateIdsConstants;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwV2.dto.LandscapePatrolDTO;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.dto.publicSecurity.PublicSecurityDTO;
import com.budwk.app.sqfwV2.dto.vo.DocumentDownVo;
import com.budwk.app.sqfwV2.dto.vo.SummaryQueryReqVo;
import com.budwk.app.sqfwV2.enums.DayingRoadFileServiceEnum;
import com.budwk.app.sqfwV2.enums.LandscapePatrolEnum;
import com.budwk.app.sqfwV2.enums.publicSecurity.PublicSecurityFileEnum;
import com.budwk.app.sqfwV2.generator.dayingRoadPdf.SafetyAllPdfGenerator;
import com.budwk.app.sqfwV2.generator.publicSecurity.pdf.PublicSecurityPdfGenerator;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 社会治安业务公共服务类
 */

@Slf4j
public class PublicSecurityComponent {
    public static List<DownFileTypesResVo> generateFiles(SummaryQueryReqVo queryReqVo, List<Sqfw_formSummary_new> formSummarys) {
        List<DownFileTypesResVo> list = new ArrayList<>();
        List<PublicSecurityFileEnum> values = PublicSecurityFileEnum.listByMark(queryReqVo.getSource());
        if (CollectionUtil.isEmpty(values)) {
            throw new AppException("数据来源参数有误！");
        }
        for (PublicSecurityFileEnum serviceEnum : values) {
            // 下载模板
            DownExcelLedgerReqVo reqVo = new DownExcelLedgerReqVo();
            reqVo.setDownTemplate(true)
                    .setAgainGenerate(queryReqVo.isAgainGenerate())
                    .setFileCode(serviceEnum.getCode())
                    .setFileType(SupportBusinessTypeEnum.PUBLIC_SECURITY.getName())
                    .setDownFileType(serviceEnum.getSuffix().equals("docx") ? "pdf" : "excel")
                    .setSource(CommonConstants.DAYROAD_PUBLICSECURITY);  // excel
            DownFileTypesResVo resVo1 = downTemplate(reqVo);
            // 组装数据返回
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            resVo.setLabel(serviceEnum.getLabel())
                    .setPreviewImage(List.of(serviceEnum.getPreviewImage().split(",")))
                    .setFileCode(String.valueOf(serviceEnum.getCode()))
                    .setHiveData(formSummarys.size() > 0)
                    .setFileTemplatePath(resVo1 != null ? resVo1.getFileTemplatePath() : null)
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(serviceEnum.getNumber());
            if (serviceEnum.getSuffix().equals("docx")) {
                resVo.setFileName(serviceEnum.getName() + ".pdf").setFileType("pdf");
                if (CollectionUtil.isNotEmpty(formSummarys)) {
                    resVo.setIds(formSummarys.stream().map(Sqfw_formSummary_new::getSqfwFlowId).distinct().collect(Collectors.toList()));
                }
            }
            if (serviceEnum.getSuffix().equals("xlsx")) {
                resVo.setFileName(serviceEnum.getName() + ".xlsx").setFileType("excel");
                if (CollectionUtil.isNotEmpty(formSummarys)) {
                    resVo.setIds(formSummarys.stream().map(Sqfw_formSummary_new::getId).distinct().collect(Collectors.toList()));
                }
            }
            list.add(resVo);
        }
        return list;
    }


    public static DownFileTypesResVo downTemplate(DownExcelLedgerReqVo reqVo) {
        String fileCode = reqVo.getFileCode();
        PublicSecurityFileEnum serviceEnum = PublicSecurityFileEnum.getByCode(fileCode);
        if (serviceEnum == null) {
            return null;
        }
        reqVo.setIds(List.of(serviceEnum.getTemplateId())).setFileName(serviceEnum.getName());
        // 下载excel模板
        if (serviceEnum.getSuffix().equals("xlsx")) {
            return DocumentDownComponent.downExcelTemplateFile(reqVo);
        }
        // 下载pdf模板
        PdfGenerateParam pdfGenerateParam = FromSummaryComponent.initPdfGenerateParam();
        pdfGenerateParam.setAbstractCustomerPdfCreateField(new PublicSecurityDTO());
        // 初始化下载参数
        DocumentDownVo documentDownVo = new DocumentDownVo();
        documentDownVo.setReqVo(reqVo).setBasePdfGenerator(new PublicSecurityPdfGenerator()).setPdfGenerateParam(pdfGenerateParam)
                .setFileName(serviceEnum.getName());
        DownFileTypesResVo resVo = DocumentDownComponent.downWordDocTemplate(documentDownVo);
        return resVo;
    }
}
